package com.example.sample.activity;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.example.app.BaseActivity;
import com.example.sample.R;
import com.example.sample.logic.LogImpl;

/**
 * 这个活动为了测试网易云信startForeground崩溃问题
 * 结论：bringToFront必须在onCreate和onStartCommand方法都调用
 */
public class ForegroundTestActivity extends BaseActivity {

    public static final String TAG = "foreground";
    Handler h = new Handler(Looper.getMainLooper());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogImpl.i(TAG, "onCreate");

        Button tv = new Button(this);
        tv.setText("start");
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startService();
            }
        });
        setContentView(tv);


        h.postDelayed(r, 2000);

        //int a = 9/0;
    }

    Runnable r = new Runnable() {
        @Override
        public void run() {
            startService();
            h.postDelayed(this, 2000);
        }
    };

    private void startService(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            LogImpl.i(TAG, "startForegroundService");
            Context context = this;
            context.startForegroundService(new Intent(context, TestService.class));
        }
    }

    public static class TestService extends Service{

        private boolean fore = false;

        @Override
        public void onCreate() {
            super.onCreate();
            LogImpl.i(TAG, "onCreate");
            bringToFront();
        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            LogImpl.i(TAG, "onStartCommand");
            bringToFront();
            return super.onStartCommand(intent, flags, startId);
        }

        private void bringToFront(){
            if (true){
                return;
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !fore){
                final String channelId = "1000001";

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                    NotificationManager notificationManager = (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
                    NotificationChannel channel = notificationManager.getNotificationChannel(channelId);
                    // 创建新渠道
                    if (channel == null){
                        channel = new NotificationChannel(channelId, "常规通知", NotificationManager.IMPORTANCE_LOW);
                        channel.enableVibration(false);
                        notificationManager.createNotificationChannel(channel);
                    }
                }

                Notification n = new NotificationCompat.Builder(getApplicationContext(), channelId)
                        .setContentTitle("a")
                        .setContentText("a")
                        .setAutoCancel(true)
                        .setContentIntent(null)
                        .setTicker("a")
                        .setSmallIcon(R.drawable.ic_launcher)
                        .build();

                startForeground(10001, n);

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        stopForeground(true);
                    }
                }, 100);
                fore = true;
            }
        }

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onDestroy() {
            LogImpl.i(TAG, "onDestroy");
            super.onDestroy();
        }
    }
}
